'use strict';

const path = require("path");
const fs = require("fs")

const Controller = require('egg').Controller;

class WxwController extends Controller {
  async menuinfo() {
    const { ctx } = this;
    ctx.body = await ctx.service.wxw.menuinfo(ctx.request.query.id);
  }
  async userinfo() {
    const { ctx } = this;
    ctx.body = await ctx.service.wxw.userinfo(ctx.request.query.userid);
  }
  async wreviewinfo() {
    const { ctx } = this;
    ctx.body = await ctx.service.wxw.wreviewinfo(ctx.request.query.cookid);
  }
  // 菜谱评论点赞
  async altercommentgood() {
    const { ctx } = this;
    if (ctx.request.query.dianzan == 1) {
      await ctx.service.wxw.altercommentgood(ctx.request.query.commentid, "+");
    }
    else {
      await ctx.service.wxw.altercommentgood(ctx.request.query.commentid, "-");
    }
    ctx.body = "修改成功";
  }
  // 菜谱收藏
  async wstorecook() {
    const { ctx } = this;
    await ctx.service.wxw.wstorecook(ctx.request.query.cookid, ctx.session.id);
    ctx.body = { code: 2101, info: "收藏成功" }
  }
  // 根据菜谱的作品
  async wcookopus() {
    const { ctx } = this;
    ctx.body = await ctx.service.wxw.wcookopus(ctx.request.query.cookid);
  }
  // 上传作品
  async waddopus() {
    const { ctx } = this;
    console.log(ctx.request.body, ctx.request.files, 6666666);
    // 上传图片路径处理
    var oldpath = this.ctx.request.files[0].filepath;
    var newpath = __dirname + "/../public/upload/" + path.basename(oldpath);
    fs.copyFile(oldpath, newpath, (err) => {
      fs.unlink(oldpath, (err) => { })
    })
    let opusimg = "/public/upload/" + path.basename(oldpath)
    ctx.body = await ctx.service.wxw.waddopus(opusimg, ctx.request.body.title, ctx.request.body.say, ctx.request.body.pushtime);
  }
  // 随便看看
  async wclassinfo() {
    const { ctx } = this;
    ctx.body = await ctx.service.wxw.wclassinfo();
  }
  // 相关
  async wrelevant() {
    const { ctx } = this;
    ctx.body = await ctx.service.wxw.wrelevant(ctx.request.query.cookid);
  }
  // 菜谱收藏数
  async wshoucanginfo() {
    const { ctx } = this;
    ctx.body = await ctx.service.wxw.wshoucanginfo(ctx.request.query.cookid);
  }
  // 是否收藏某商品
  async wifstore() {
    const { ctx } = this;
    let res = await ctx.service.wxw.wifstore(ctx.session.id, ctx.request.query.cookid);
    if (res[0]) {
      ctx.body = { code: 3100, info: "已收藏该商品" }
    } else {
      ctx.body = { code: 3101, info: "未收藏该商品" }
    }
  }

  // 个人页面
  // 获取用户信息
  async wuserinfo() {
    const { ctx } = this;
    ctx.body = await ctx.service.wxw.wuserinfo(ctx.session.id);
  }
  // 修改用户信息
  async walteruser() {
    const { ctx } = this;
    if (ctx.request.files) {
      // 上传头像文件
      var oldpath = this.ctx.request.files[0].filepath;
      var newpath = __dirname + "/../public/upload/" + path.basename(oldpath);
      fs.copyFile(oldpath, newpath, (err) => {
        fs.unlink(oldpath, (err) => { })
      })
      let touxiang = "/public/upload/" + path.basename(oldpath)
      ctx.body = await ctx.service.wxw.walteruser(ctx.session.id, touxiang);
    }
    else if (ctx.request.body.email) {
      ctx.body = await ctx.service.wxw.walteruseremail(ctx.session.id, ctx.request.body.email, ctx.request.body.pwd);
    }
    else {
      ctx.body = await ctx.service.wxw.walteruserinfo(ctx.session.id, ctx.request.body.username, ctx.request.body.say, ctx.request.body.address, ctx.request.body.sex);
    }
  }


  // 移动端
  async wcookinfo() {
    const { ctx } = this;
    ctx.body = await ctx.service.wxw.wcookinfo();
  }

}

module.exports = WxwController;